var page = 1;
var pageOld = 1;
var pageNum = 10;
var pageI = 1;
var pageL = 5;
var updateDeleteUserId = 0;
var updateUserStatus = 0;
var isUpdateOrDelete = 0;

var systemRecomTeamFunction = function(){
    $.ajax({
        url:"usersWeb/findAllRecommendByUserIdPage.json",
        type:'POST',
        dataType:"json",
        data:{
            page:page,pageNum:pageNum
        },
        success:function(result){
            var tem = "<tr>" +
                "<th width='10%'><span>序号</span></th>" +
                "<th width='18%'><span>被推荐人</span></th>" +
                "<th width='18%'><span>职业</span></th>" +
                "<th width='18%'><span>权重</span></th>" +
                "<th width='18%'><span>状态</span></th>" +
                "<th width='18%'><span>详情</span></th>" +
                "</tr>";
            $.each( result.recommendList, function(index, content){
                tem += "<tr>" +
                    "<td><span class='tdSpan'>"+index+"</span></td>" +
                    "<td><span class='tdSpan'>"+content.userName+"</span></td>" +
                    "<td><span class='tdSpan'>"+content.rolename+"</span></td>" +
                    "<td><span class='tdSpan'>"+content.sort+"</span></td>";
                    if(content.status == 1){
                        tem += "<td><span class='tdSpan'>已启用</span></td>";
                    }else if(content.status == 2){
                        tem += "<td><span class='tdSpan'>已禁用</span></td>";
                    }else {
                        tem += "<td><span class='tdSpan'>异常</span></td>";
                    }
                tem += "<td><span class='tdSpan'>" +
                    "<a href=\"#\" class='newEm' onclick=\"editUserFunction("+content.id+",'"+content.userName+"','"+content.sort+"');\">编辑</a>" +
                    "<a href=\"#\" class='deleteEm' onclick=\"deleteFunction("+content.id+");\">删除</a>";
                    if(content.status == 1){
                        tem +=  "<a href=\"#\" class='deleteEm' onclick=\"onBlackFunction("+content.id+",2);\">禁用</a>";
                    }else {
                        tem +=  "<a href=\"#\" class='deleteEm' onclick=\"onBlackFunction("+content.id+",1);\">启用</a>";
                    }
                tem += "</span></td>" +
                    "</tr>";
            });
            $("#systemRecomTable").empty();
            $("#systemRecomTable").append(tem);

            var pageSum = result.pageSum;
            var numSum = result.numSum;
            var temPage ="<a href='#' onclick='pageFunction("+page+","+page+",\"em\","+pageSum+");'><em>&lt;&lt;</em></a>";

            if(pageSum<=5){
                pageL = pageSum;
            }else if(pageSum>5 && pageSum-pageI < 4 || pageSum-page < 4){
                pageI = pageSum-4;
                pageL = pageSum;
            }else {
                if(page >= 5){
                    if(page > pageOld){
                        if(page-pageI == 4){
                            pageI = page;
                            pageL = page+4;
                        }
                    }else if(page < pageOld){
                        if(pageL-page == 4){
                            pageI = page-4;
                            pageL = page;
                        }
                    }
                }else {
                    pageI = 1;
                    pageL = pageI+4;
                }
            }

            for(var i=pageI;i<=pageL;i++){
                if(page == i){
                    temPage += "<a class='cur' href='#' onclick='pageFunction("+i+","+page+",\"onc\");'>"+i+"</a>";
                }else {
                    temPage += "<a href='#' onclick='pageFunction("+i+","+page+",\"onc\");'>"+i+"</a>";
                }
            }

            temPage += "<a href='#' onclick='pageFunction("+page+","+page+",\"ep\","+pageSum+");'><em>&gt;&gt;</em></a>" +
                "<span>共"+numSum+"条/"+pageSum+"页</span>" +
                "<input class='pageTxt' name='pageValue' type='text' value='' />" +
                "<input class='pageBut' type='button' value='GO' onclick='pageFunction(0,"+page+",\"select\");'/>";

            $("#systemRecomPage").empty();
            $("#systemRecomPage").append(temPage);

            //动态加class
            $(".page a,.orderTit a").click(function(){
                $(this).addClass("cur").siblings().removeClass("cur");
            });
            $(".authP label").click(function(){
                $(this).toggleClass("cur");
            });
            $(".userDiv ul li p label").click(function(){
                $(this).toggleClass("on");
            });

            //新建角色
            $(".newEm").click(function(){
                $(".deskDiv").css({
                    display:"block",height:$(document).height()
                });
                $(".roleNew,.teamDiv").css({
                    top:($(window).height()-$(".roleNew,.teamDiv").height())/2+$(window).scrollTop()+"px",
                    display:"block"
                });
            });
            //删除角色
            $(".deleteEm").click(function(){
                $(".deskDiv").css({
                    display:"block",height:$(document).height()
                });
                $(".roleDele").css({
                    top:($(window).height()-$(".roleDele").height())/2+$(window).scrollTop()+"px",
                    display:"block"
                });
            });
            //重置密码
            $(".resetPass,.disableA,.finished,.callOrder").click(function(){
                $(".deskDiv").css({
                    display:"block",height:$(document).height()
                });
                $(".hints").css({
                    top:($(window).height()-$(".hints").height())/2+$(window).scrollTop()+"px",
                    display:"block"
                });
            });
        }
    });
};

systemRecomTeamFunction();

//禁用，启用，删除
var updateDeleteRecomFunction = function () {
    if(isUpdateOrDelete == 0){
        $.ajax({
            url:"usersWeb/deleteByPrimaryKeyRecommed.json",
            type:'POST',
            dataType:"json",
            data:{
                recommedId:updateDeleteUserId
            },
            success:function(result){
                if(result.isOK == 'OK'){
                    systemRecomTeamFunction();
                }else {
                    hintsPValueFucntion(result.msg);
                }
            },
            error:function(){
                hintsPValueFucntion("请求失败");
            }
        });
    }else {
        $.ajax({
            url:"usersWeb/insertSelectiveRecommend.json",
            type:'POST',
            dataType:"json",
            data:{
                id:updateDeleteUserId,status:updateUserStatus
            },
            success:function(result){
                if(result.isOK == 'OK'){
                    systemRecomTeamFunction();
                }else {
                    hintsPValueFucntion(result.msg);
                }
            },
            error:function(){
                hintsPValueFucntion("请求失败");
            }
        });
    }
}

//表单
var savaRecomFunction = function () {
    $.ajax({
        url:"usersWeb/insertSelectiveRecommend.json",
        type:'POST',
        dataType:"json",
        data:$("#systemRecomForm").serialize(),
        success:function(result){
            if(result.isOK == 'OK'){
                systemRecomTeamFunction();
            }else {
                hintsPValueFucntion(result.msg);
            }
        },
        error:function(){
            hintsPValueFucntion("请求失败");
        }
    });
}

//编辑按钮点击事件
var editUserFunction = function(i,u,s){
    $("input[name='id']").val(i);
    $("input[name='username']").val(u);
    $("input[name='sort']").val(s);
}

//删除按钮点击事件
var deleteFunction = function(i){
    updateDeleteUserId = i;
    isUpdateOrDelete = 0;
}

//禁用/启用按钮点击事件
var onBlackFunction = function(i,s){
    updateDeleteUserId = i;
    updateUserStatus = s;
    isUpdateOrDelete = 1;
}

var pageFunction = function(p,o,t,s){
    if(t == 'select'){
        if(parseInt($("input[name='pageValue']").val()) < 1){
            page = 1;
        }else {
            page = parseInt($("input[name='pageValue']").val());
        }
        pageI = page - 2;
        pageL = page + 2;
    }else if(t == 'em'){
        if(p == 1){
            page = p;
        }else {
            page = p - 1;
        }
        if(p==5 || pageL-p == 4){
            pageI = p-4;
            pageL = p;
        }
    }else if(t == 'ep'){
        if(p == s){
            page = p;
        }else {
            page = p + 1;
        }
        if(p==5 || p-pageI == 4){
            pageI = p;
            pageL = p+4;
        }
    }else {
        page = p ;
    }
    systemDictionListFuntion();
    pageOld = o;
}

//友情提示
var hintsPValueFucntion = function(v){
    $("#hintsPValue").html(v);
    $(".hints").css({
        top:($(window).height()-$(".hints").height())/2+$(window).scrollTop()+"px",
        display:"block"
    });
}

//关闭
$(".cancel,.confirm").click(function(){
    $(".deskDiv").fadeOut();
    $(".roleDele").fadeOut();
    $(".roleNew").fadeOut();
    $(".hints").fadeOut();
    $(".authority").fadeOut();
    $(".modifyOrder").fadeOut();
    $(".hintsDiv").fadeOut();
    $(".teamDiv").fadeOut();
    $(".refundDiv").fadeOut();
    $(".addNote").fadeOut();
    $(".evaluation").fadeOut();
});
$(".submit").click(function(){
    $(".deskDiv").fadeOut();
    $("#change").fadeOut();
    $("#change1").fadeOut();
});